from django.conf import settings
from django.core.mail import send_mail

from celery_tasks.celery import app
from my_taobao.libs.yuntongxun.sms import CCP


@app.task(bind=True)
def send_message(self,mobile,sms_code):
    # import time
    # time.sleep(10)
    try:
        result=CCP().send_template_sms(mobile,[sms_code,5],1)
    except:
        result=-1

    if result==-1:
        self.retry(exc=Exception("发送短信失败"), countdown=5, max_retries=3)


@app.task(bind=True)
def send_verify_email(self,verify_url,email):
    #1 发送短信
    result=-1
    try:
        result = send_mail(subject='美多商城，验证链接', recipient_list=[email],
                           from_email=settings.EMAIL_FROM,message=verify_url)
        # if  result!=1:
        #     return http.JsonResponse({"code": 500, "errmsg": '发送邮件失败'})
    except Exception as e:
        result=-1

    if result==-1:
        self.retry(exc=Exception('发送邮件失败'),countdown=5,max_retries=3)